package com.software.project_view.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.project_view.entity.Block;
import com.software.project_view.mapper.BlockMapper;
import com.software.project_view.service.BlockService;
import com.software.project_view.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlockServiceImpl extends ServiceImpl<BlockMapper, Block> implements BlockService {

    @Autowired
    private BlockMapper blockMapper;

    private static final Integer DIFFICULTY = 5;

    public R genesisBlock() {
        Block block = new Block("创世区块", "0");
        block.mineBlock(DIFFICULTY);
        int insert = blockMapper.insert(block);
        if (insert > 0) {
            return R.success().message("success");
        }
        return R.error().message("fail");
    }

    public R getRecentBlock() {
        Block block = blockMapper.selectRecent();
        return R.success().data("block", block);
    }

    public R getBlockAll() {
        QueryWrapper<Block> wrapper = new QueryWrapper<>();
        List<Block> blocks = blockMapper.selectList(wrapper);
        return R.success().data("blocks", blocks);
    }

}
